//导航守卫
import { useGlobalUserStore } from "@/store/userGlobalStore.js";
import { useGlobalMenuStore } from "@/store/menuGlobalStore";
export async function beforeEach(to, from, next) {
  // 如果是/login设置页面title
  if (to.path === "/login") {
    document.title = "小鹿线基础权限框架";
    next()
  }
  // 判断用户是否登录
  if (to.path !== "/login" && !localStorage.getItem("TOKEN")) {
    next("/login");
  }
  
  //如果可以获取到用户信息，就是正常访问
  try{
    const store = useGlobalUserStore();
    await store.getGlobalUserInfo()
    const { permissions } = useGlobalUserStore();
    if(permissions[0] === '*:*:*'){ //超级管理员
        next()
    }
    // 判断权限
    let {authSlideMenuMap} = useGlobalMenuStore()
    if(!authSlideMenuMap.get(to.path)){
      ElMessage.error('您没有权限访问该页面')
      return false
    }else{
      return true
    }
   
  }catch(err){
    next("/login")
  }
}
export function afterEach(to, from) {}
